/*     */ package com.controller;


/*     */

import com.domain.Book;
import com.domain.BookLate;
import com.domain.Student;
import com.domain.StudentBook;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.IBookService;
import com.service.IStuBookService;
import com.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*     */
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @Controller
/*     */ @RequestMapping({"/students"})
/*     */ public class StudentController
/*     */ {
/*     */   @Autowired
/*     */   private IStudentService studentService;
/*     */   @Autowired
/*     */   private IBookService bookService;
/*     */   @Autowired
/*     */   private IStuBookService stuBookService;
/*     */   
/*     */   @RequestMapping({"/findAll"})
/*     */   public ModelAndView findAll(@RequestParam(name = "page", defaultValue = "1", required = false) int page, @RequestParam(name = "size", defaultValue = "4", required = false) int size) throws Exception {
/*  46 */     ModelAndView mv = new ModelAndView();
/*  47 */     if (size == 0) {
/*  48 */       size = 4;
/*     */     }
/*  50 */     List<Student> studentList = this.studentService.findAll(page, size);
/*  51 */     PageInfo pageInfo = new PageInfo(studentList);
/*  52 */     mv.addObject("pageInfo", pageInfo);
/*  53 */     mv.setViewName("/student/student-list");
/*  54 */     return mv;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/save"})
/*     */   public ModelAndView save(Student student) throws Exception {
/*  66 */     ModelAndView mv = new ModelAndView();
/*     */     
/*  68 */     List<Student> studentList = this.studentService.findByName(student.getName());
/*  69 */     if (studentList.size() == 0) {
/*  70 */       this.studentService.save(student);
/*  71 */       String name = student.getName();
/*  72 */       mv.setViewName("redirect:/students/findByName?name=" + URLEncoder.encode(name, "UTF-8"));
/*  73 */       return mv;
/*     */     } 
/*  75 */     List<Student> studentList1 = new ArrayList<>();
/*  76 */     studentList1.add(student);
/*  77 */     mv.addObject("msg", "名字重复");
/*  78 */     mv.addObject("students", studentList1);
/*  79 */     mv.setViewName("/student/student-add-error");
/*  80 */     return mv;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/delete"})
/*     */   public ModelAndView delete(@RequestParam(name = "id") Integer id, @RequestParam(name = "page") Integer page, @RequestParam(name = "size") Integer size) throws Exception {
/*  94 */     ModelAndView mv = new ModelAndView();
/*  95 */     this.studentService.delete(id);
/*  96 */     this.studentService.updateSid(id);
/*  97 */     this.studentService.autoIncrement();
/*  98 */     mv.setViewName("redirect:/students/findAll?page=" + page + "&size=" + size);
/*  99 */     return mv;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/toUpdate"})
/*     */   public String toUpdate(Integer id, Model model) throws Exception {
/* 112 */     Student student = this.studentService.findById(id);
/* 113 */     model.addAttribute("student", student);
/* 114 */     return "/student/student-update";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/update"})
/*     */   public String update(Student student) throws Exception {
/* 126 */     this.studentService.update(student);
/* 127 */     return "redirect:/students/findAll";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/showInfo"})
/*     */   public String showInfo(Integer id, Model model) throws Exception {
/* 140 */     Student student = this.studentService.findById(id);
/* 141 */     model.addAttribute("student", student);
/* 142 */     return "/student/show-info";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/findAllBook"})
/*     */   public ModelAndView findAllBook(@RequestParam(name = "page", defaultValue = "1", required = false) Integer page, @RequestParam(name = "size", defaultValue = "4", required = false) Integer size) {
/* 155 */     ModelAndView mv = new ModelAndView();
/* 156 */     List<Book> books = this.bookService.findAll(page.intValue(), size.intValue());
/* 157 */     List<String> booktype = this.bookService.findAllBookType();
/* 158 */     PageInfo pageInfo = new PageInfo(books);
/* 159 */     mv.addObject("pageInfo", pageInfo);
/* 160 */     mv.addObject("bookTypes", booktype);
/* 161 */     mv.setViewName("/student/select-book");
/* 162 */     return mv;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/findByType"})
/*     */   public ModelAndView findByType(@RequestParam(name = "type") String type) {
/* 173 */     ModelAndView mv = new ModelAndView();
/* 174 */     List<Book> books = this.bookService.findByType(type);
/* 175 */     List<String> booktype = this.bookService.findAllBookType();
/* 176 */     PageInfo pageInfo = new PageInfo(books);
/* 177 */     mv.addObject("pageInfo", pageInfo);
/* 178 */     mv.addObject("bookTypes", booktype);
/* 179 */     mv.setViewName("/student/select-book");
/* 180 */     return mv;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/findByBName"})
/*     */   public String findByBName(@RequestParam(name = "type") String bookName, Model model) throws Exception {
/* 193 */     List<Book> book = this.bookService.findMoHu("%" + bookName + "%");
/* 194 */     String error = "";
/* 195 */     if (book.size() == 0) {
/* 196 */       error = "未找到!";
/*     */     }
/* 198 */     model.addAttribute("error", error);
/* 199 */     model.addAttribute("books", book);
/* 200 */     return "/student/select_item_book";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/borrowBook"})
/*     */   public ModelAndView borrowBook(@RequestParam(name = "sid") Integer studentId, @RequestParam(name = "bid") Integer bookId) throws Exception {
/* 214 */     List<StudentBook> lateBook = this.stuBookService.lateBook(studentId);
/* 215 */     String text = "";
/* 216 */     ModelAndView mv = new ModelAndView();
/* 217 */     if (lateBook.size() != 0) {
/* 218 */       text = "你当前有未归还的图书，请先进行归还！";
/* 219 */       mv.addObject("text", text);
/* 220 */       mv.setViewName("redirect:/students/findLate?id=" + studentId);
/*     */     } else {
/* 222 */       Student student = this.studentService.findById(studentId);
/* 223 */       Book book = this.bookService.findById(bookId);
/* 224 */       StudentBook studentBook = new StudentBook();
/* 225 */       Date deadTime = new Date();
/* 226 */       Date borrowTime = new Date();
/* 227 */       deadTime.setTime(borrowTime.getTime() + 2592000000L);
/* 228 */       studentBook.setBookId(book.getBid());
/* 229 */       studentBook.setStuId(student.getSid());
/* 230 */       studentBook.setBorrowTime(borrowTime);
/* 231 */       studentBook.setDeadTime(deadTime);
/* 232 */       this.stuBookService.save(studentBook);
/* 233 */       mv.setViewName("redirect:/students/nowBorrow?id=" + studentId);
/*     */     } 
/* 235 */     return mv;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/nowBorrow"})
/*     */   public String nowBorrow(@RequestParam(name = "id") Integer studentId, Model model, @RequestParam(name = "text", required = false) String text) throws Exception {
/* 248 */     String message = "";
/* 249 */     List<StudentBook> studentBookList = this.stuBookService.findBookByStuId(studentId);
/* 250 */     model.addAttribute("studentBookList", studentBookList);
/* 251 */     if (studentBookList.size() == 0) {
/* 252 */       message = "你还没有借任何书";
/*     */     }
/* 254 */     model.addAttribute("message", message);
/* 255 */     model.addAttribute("text", text);
/* 256 */     return "/student/now-borrow";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/returnBook"})
/*     */   public ModelAndView returnBook(@RequestParam(name = "sid") Integer studentId, @RequestParam(name = "bid", required = true) Integer bookId, @RequestParam(name = "SBId") Integer stuBookId) throws Exception {
/* 271 */     ModelAndView mv = new ModelAndView();
/* 272 */     StudentBook studentBook = new StudentBook();
/* 273 */     studentBook.setId(stuBookId);
/* 274 */     studentBook.setStuId(studentId);
/* 275 */     studentBook.setBookId(bookId);
/* 276 */     Date returnTime = new Date();
/* 277 */     studentBook.setReturnTime(returnTime);
/* 278 */     this.stuBookService.update(studentBook);
/* 279 */     System.out.println("执行了....");
/* 280 */     System.out.println(studentId);
/* 281 */     mv.setViewName("redirect:/students/nowBorrow?id=" + studentId);
/* 282 */     return mv;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/updatePassword"})
/*     */   public String updatePassword(@RequestParam(name = "id") Integer id, @RequestParam(name = "pwd2") String pwd, @RequestParam("oldPassword") String oldPassword, Model model) throws Exception {
/* 300 */     String oldpwd = this.studentService.getPasswordById(id);
/* 301 */     if (oldpwd.equals(oldPassword)) {
/* 302 */       Student student = new Student();
/* 303 */       student.setSid(id);
/* 304 */       student.setPassword(pwd);
/* 305 */       this.studentService.updatePassword(student);
/* 306 */       return "main";
/*     */     } 
/* 308 */     model.addAttribute("msg", "密码输入错误");
/* 309 */     return "/student/update-password";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/history"})
/*     */   public String history(@RequestParam(name = "id") Integer studentId, Model model) throws Exception {
/* 322 */     List<StudentBook> studentBooks = this.stuBookService.findHistoryByStuId(studentId);
/* 323 */     String text = null;
/* 324 */     if (studentBooks.size() == 0) {
/* 325 */       text = "未找到你有任何借阅历史！";
/*     */     }
/* 327 */     model.addAttribute("text", text);
/* 328 */     model.addAttribute("studentBooks", studentBooks);
/* 329 */     return "/student/history";
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/xuJie"})
/*     */   public ModelAndView xuJie(@RequestParam(name = "id") Integer stuBookId) throws Exception {
/* 341 */     ModelAndView mv = new ModelAndView();
/* 342 */     String text = "";
/* 343 */     StudentBook studentBook = this.stuBookService.findById(stuBookId);
/* 344 */     Date deadTime = studentBook.getDeadTime();
/* 345 */     deadTime.setTime(deadTime.getTime() + 2592000000L);
/* 346 */     Date borrowTime = studentBook.getBorrowTime();
/* 347 */     borrowTime.setTime(borrowTime.getTime() + 10368000000L);
/* 348 */     if (deadTime.getTime() == borrowTime.getTime()) {
/* 349 */       System.out.println("你已续借两次，不能再进行续借");
/* 350 */       text = "你已续借两次，不能再进行续借";
/*     */     } else {
/* 352 */       studentBook.setDeadTime(deadTime);
/* 353 */       this.stuBookService.updateXuJie(studentBook);
/*     */     } 
/* 355 */     Integer studentId = studentBook.getStuId();
/* 356 */     mv.setViewName("redirect:/students/nowBorrow?id=" + studentId + "&text=" + URLEncoder.encode(text, "UTF-8"));
/* 357 */     return mv;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/findByName"})
/*     */   public ModelAndView findByName(@RequestParam(name = "name") String name) throws Exception {
/* 369 */     ModelAndView mv = new ModelAndView();
/*     */     
/* 371 */     List<Student> student = this.studentService.findMoHu("%" + name + "%");
/* 372 */     if (student.size() == 0) {
/* 373 */       String message = "未找到啊！";
/* 374 */       mv.addObject("message", message);
/*     */     } 
/* 376 */     PageHelper.startPage(1, 4);
/* 377 */     PageInfo pageInfo = new PageInfo(student);
/* 378 */     mv.addObject("pageInfo", pageInfo);
/* 379 */     mv.setViewName("/student/student-list");
/* 380 */     return mv;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @RequestMapping({"/findLate"})
/*     */   public ModelAndView findLate(@RequestParam(name = "id") Integer studentId) throws Exception {
/* 392 */     ModelAndView mv = new ModelAndView();
/* 393 */     Date date = new Date();
/* 394 */     String message = "";
/* 395 */     List<StudentBook> studentBooks1 = this.stuBookService.lateBook(studentId);
/* 396 */     List<BookLate> bookLates = new ArrayList<>();
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     
/* 403 */     if (studentBooks1.size() == 0) {
/* 404 */       message = "你当前没有超期图书!";
/* 405 */       System.out.println("你好!!!" + message);
/*     */     } else {
/*     */       
/* 408 */       for (StudentBook studentBook : studentBooks1) {
/* 409 */         if (studentBook.getReturnTime() == null) {
/* 410 */           BookLate bookLate = new BookLate();
/* 411 */           bookLate.setBorrowTime(studentBook.getBorrowTime());
/* 412 */           bookLate.setDeadTime(studentBook.getDeadTime());
/* 413 */           String bookName = this.bookService.findById(studentBook.getBookId()).getName();
/* 414 */           bookLate.setBookName(bookName);
/* 415 */           String stuName = this.studentService.findById(studentBook.getStuId()).getName();
/* 416 */           bookLate.setName(stuName);
/* 417 */           bookLates.add(bookLate);
/*     */         } 
/*     */       } 
/* 420 */       mv.addObject("bookLate", bookLates);
/*     */     } 
/*     */     
/* 423 */     mv.addObject("message", message);
/* 424 */     mv.setViewName("/student/late");
/* 425 */     return mv;
/*     */   }
/*     */ }


/* Location:              G:\项目\图书管理系统\library_system-1.0-SNAPSHOT.war!\WEB-INF\classes\com\controller\StudentController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */